package com.zhss.eshop.commodity.controller;

import com.zhss.eshop.common.constant.CloneDirection;
import com.zhss.eshop.commodity.domain.dto.CommodityCategoryDTO;
import com.zhss.eshop.commodity.domain.dto.CommodityCategoryPropertyRelationshipDTO;
import com.zhss.eshop.commodity.domain.dto.CommodityPropertyGroupDTO;
import com.zhss.eshop.commodity.domain.dto.CommodityPropertyGroupRelationshipDTO;
import com.zhss.eshop.commodity.domain.vo.CommodityCategoryVO;
import com.zhss.eshop.commodity.domain.vo.CommodityPropertyGroupVO;
import com.zhss.eshop.commodity.service.CommodityCategoryService;
import com.zhss.eshop.common.util.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 类目管理controller组件
 * @author suyuan
 *
 */
@RestController
@RequestMapping("/commodity/category")
public class CategoryController {

	private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);

	/**
	 * 类目管理service组件
	 */
	@Autowired
	private CommodityCategoryService categoryService;

	/**
	 * 查询根类目
	 * @return 根类目集合
	 */
	@GetMapping("/root")
	public List<CommodityCategoryVO> listRoots() {
		try {
			List<CommodityCategoryVO> resultCategories = ObjectUtils.convertList(
					categoryService.listRoots(), CommodityCategoryVO.class);
			return resultCategories;
		} catch (Exception e) {
			logger.error("error", e);
			return new ArrayList<CommodityCategoryVO>();
		}
	}

	/**
	 * 查询子类目
	 * @return 子类目集合
	 */
	@GetMapping("/children/{id}")
	public List<CommodityCategoryVO> listChildren(@PathVariable("id") Long id) {
		try {
			List<CommodityCategoryVO> resultCategories = ObjectUtils.convertList(
					categoryService.listChildren(id), CommodityCategoryVO.class);
			return resultCategories;
		} catch (Exception e) {
			logger.error("error", e);
			return new ArrayList<CommodityCategoryVO>();
		}
	}

	/**
	 * 新增类目
	 * @param category 类目
	 * @return 处理结果
	 */
	@PostMapping("/")
	public Boolean save(@RequestBody CommodityCategoryVO category) {
		try {
			// 转换类目基本信息
			CommodityCategoryDTO targetCategory = category.clone(CommodityCategoryDTO.class);

			// 转换类目与属性的关联关系
			List<CommodityCategoryPropertyRelationshipDTO> targetPropertyRelations = ObjectUtils.convertList(
					category.getPropertyRelations(), CommodityCategoryPropertyRelationshipDTO.class);
			targetCategory.setPropertyRelations(targetPropertyRelations);

			// 转换属性分组
			if(category.getPropertyGroups() != null) {
				List<CommodityPropertyGroupDTO> targetPropertyGroups = new ArrayList<CommodityPropertyGroupDTO>();
				targetCategory.setPropertyGroups(targetPropertyGroups);

				for(CommodityPropertyGroupVO group : category.getPropertyGroups()) {
					CommodityPropertyGroupDTO targetGroup = group.clone(CommodityPropertyGroupDTO.class);
					targetGroup.setRelations(ObjectUtils.convertList(
							group.getRelations(), CommodityPropertyGroupRelationshipDTO.class));
					targetPropertyGroups.add(targetGroup);
				}
			}

			// 执行类目新增的操作
			categoryService.insertSelective(targetCategory);

			return true;
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
	}

	/**
	 * 根据id查询类目
	 * @param id 类目id
	 * @return 类目
	 */
	@GetMapping("/{id}")
	public CommodityCategoryVO getById(@PathVariable("id") Long id) {
		try {
			CommodityCategoryDTO category = categoryService.getById(id);
			CommodityCategoryVO resultCategory = category.clone(
					CommodityCategoryVO.class, CloneDirection.OPPOSITE);
			return resultCategory;
		} catch (Exception e) {
			logger.error("error", e);
			return null;
		}
	}

	// TODO: 2021/3/8  更新类目、删除类目

	/**
	 * 删除类目
	 * @param id 类目id
	 * @return 处理结果
	 */
	@DeleteMapping("/{id}")
	public Boolean remove(@PathVariable("id") Long id) {
		try {
			return categoryService.remove(id);
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
	}

}
